<template>
	<view>
		<!-- 资源列表显示 -->
		<view class="resouerces_list" v-for="(item,index) in resource_list" :key="index">
			<view class="resouerces_left">
				<image style="border-radius: 10rpx;" :src="item.image" mode="" v-if="item.type == 1 "
					@click="previewImage(item.image,0)"></image>
				<image
					src="https://wanchuyun-img.oss-cn-beijing.aliyuncs.com/guomai/92106d49d9a81ca954c7ef03df2a53bcb632f84e.png"
					mode="" v-else></image>
				<view class="resouerces_content">
					<text class="tit">{{item.title}}</text>
					<view class="content">
						<text class="time">{{item.create_time}}</text>
						<text class="other">|</text>
						<text>{{item.size}}</text>
					</view>
				</view>
			</view>

			<view class="resouerces_right line_center" @click="download_resource(item.image,item.type)">
				下载
			</view>
		</view>

	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				resource_list: []
			};
		},
		onLoad() {
			this.getResource()
		},
		methods: {
			// 获取资源
			getResource() {
				let that = this;
				app.globalRequest({
					api: app.getApi().user.resources,
				}).then(res => {
					if (res.code == 1) {
						that.resource_list = res.data
					}
				}).catch(err => {

				})
			},

			// 预览图片
			previewImage: function(img, index) {
				console.log(img)
				var arr = []
				arr.push(img); //获取当前页面的轮播图数据
				//uniapp预览轮播图
				uni.previewImage({
					current: index, //预览图片的下标
					urls: arr //预览图片的地址，必须要数组形式，如果不是数组形式就转换成数组形式就可以
				})
			},

			// 下载图片和视频
			download_resource(url, type) {
				uni.showLoading({
					title: '下载中...'
				})
				uni.downloadFile({
					url: url,
					success: res => {
						const {
							statusCode,
							tempFilePath
						} = res
						if (statusCode === 200) { // saveImageToPhotosAlbum  saveVideoToPhotosAlbum  
							uni.saveFile({
								tempFilePath: tempFilePath,
								success: data => {
									if (type == 1) {
										uni.saveImageToPhotosAlbum({
											filePath: data.savedFilePath,
											success: data => {

												if (data.errMsg ==
													'saveImageToPhotosAlbum:ok') {
													uni.hideLoading()
													uni.showToast({
														mask: true,
														title: '保存相册成功'
													});
												}
												// that.showToast('下载成功,文件已保存到' + data.path);
											}
										});
									} else {
										uni.saveVideoToPhotosAlbum({
											filePath: data.savedFilePath,
											success: data => {

												if (data.errMsg ==
													'saveVideoToPhotosAlbum:ok') {
													uni.hideLoading()
													uni.showToast({
														mask: true,
														title: '保存相册成功'
													});
												}
												// that.showToast('下载成功,文件已保存到' + data.path);
											}
										});
									}
								},
								fail: (errMsg) => {
									uni.showToast({
										mask: true,
										title: '保存失败'
									});
								},
								complete: (errMsg) => {

								}
							});
						} else {
							// that.showToast('下载失败');
						}
					},
					complete: () => {

					}
				})

			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.resouerces_list {
		width: 92%;
		margin: auto;
		padding: 20rpx 0;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 2rpx solid #f6f6f6;

		.resouerces_left {
			display: flex;
			align-items: center;

			image {
				width: 86rpx;
				height: 86rpx;
				margin-right: 20rpx;
			}

			.resouerces_content {
				display: flex;
				flex-direction: column;

				.tit {
					color: #060606;
					font-size: 28rpx;
				}

				.content {
					display: flex;
					align-items: center;
					margin-top: 5rpx;

					.other {
						margin: 0 20rpx;
					}

					text {
						color: #878FA1;
						font-size: 22rpx;
					}
				}
			}
		}

		.resouerces_right {
			width: 88rpx;
			height: 44rpx;
			background: #639EF6;
			border-radius: 22rpx;
			color: #fff;
		}
	}
</style>